import { createRouter,createWebHistory } from "vue-router"
import {checkToken} from "../http/login_register.js"
import login from "../views/login/login.vue"
import register from "../views/register/register.vue"
import main from "../views/main/main.vue"
import data_analysis from "../views/main_main/data_analysis.vue"
import data_entry from "../views/main_main/data_entry.vue"
import data_statistic from "../views/main_main/data_statistic.vue"
import wages from "../views/main_main/wages.vue"
import Industrys_trend from "../views/main_main/Industrys_trend.vue"
import settings from "../views/main_main/settings.vue"
const routes = [
	{
		path: '/',
		name:"main",
		component:main,
		children:[
			{
				path: 'data_analysis',
				name:"data_analysis",
				component:data_analysis
			},
			{
				path: 'data_entry',
				name:"data_entry",
				component:data_entry
			},
			{
				path: 'data_statistic',
				name:"data_statistic",
				component:data_statistic
			},
			{
				path: 'wages',
				name:"wages",
				component:wages
			},
			{
				path: 'Industrys_trend',
				name:"Industrys_trend",
				component:Industrys_trend
			},
			{
				path: 'settings',
				name:"settings",
				component:settings
			}
		]
	},
	//登陆
	{ 
		path: '/login',
		name:"login",
		component:login,		
	},
	//注册
	{
		path: '/register',
		name:"register",
		component:register
	},	
]


const router = createRouter({
  history:createWebHistory(),
  routes,
});

router.beforeEach((to,from,next)=>{
	if(to.path.startsWith("/login") || to.path.startsWith("/register")){
	  	window.sessionStorage.removeItem('access-admin')
	  	next()
	}else{
		let admin = JSON.parse(window.sessionStorage.getItem("access-admin"))
		if(!admin){
			alert("登录校验失败，请重新登录")
			next({path:'/login'})
		}else{
			checkToken().then(res=>{
				if(!res.data){
				  	alert("校验登录失败")
				  	next({ path:'/login' })
				}
				next()
			})
		}
	}
})

export default router;